/**
 * Copyright (C) 2017  Alibaba Group Holding Limited.
 */

#include "ls_hal.h"
#include "hal_test.h"

#if defined(CONFIG_HAL_CRYPTO)

#define TEST_ECB_SIZE  (96)
#define TEST_CBC_SIZE  (96)
#define TEST_CTR_SIZE  (141)
#define TEST_CFB_SIZE  (141)
#define TEST_DATA_SIZE (141)

static const uint8_t _g_aes_key[32] = {
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x0f,
   0x0f, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
   0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
};

static const uint8_t _g_aes_iv[16] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};

static const uint8_t _g_test_data[TEST_DATA_SIZE] = {
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01,
   0x02, 0x03, 0x04, 0x05, 0x13
};

/* openssl calculated results */
static const uint8_t _g_ecb_128_enc[TEST_ECB_SIZE] = {
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7,
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7,
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7,
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7,
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7,
    0x55, 0x4c, 0x93, 0x50, 0x16, 0x35, 0xaa, 0xdb,
    0xad, 0xf8, 0x8b, 0xc6, 0xd3, 0x6e, 0x57, 0xf7
};

static const uint8_t _g_ecb_192_enc[TEST_ECB_SIZE] = {
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a,
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a,
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a,
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a,
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a,
    0x7c, 0x73, 0x26, 0x78, 0x4d, 0x71, 0xaf, 0xaf,
    0x2b, 0x1a, 0xae, 0x70, 0xe3, 0x1f, 0x6f, 0x4a
};

static const uint8_t _g_ecb_256_enc[TEST_ECB_SIZE] = {
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53,
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53,
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53,
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53,
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53,
    0x34, 0xad, 0x17, 0xeb, 0x50, 0xfb, 0x58, 0xda,
    0x3b, 0x60, 0x18, 0x18, 0x0f, 0x70, 0x13, 0x53
};

static const uint8_t _g_cbc_128_enc[TEST_ECB_SIZE] = {
    0xe4, 0x57, 0x7c, 0x54, 0x84, 0x50, 0x31, 0x40, 0x6b, 0x6d, 0x76, 0x85, 0x17, 0xc2, 0x2c, 0x74, 
    0x9d, 0x24, 0xd4, 0xd8, 0x4e, 0x51, 0x44, 0xc1, 0x41, 0xcb, 0x62, 0xb5, 0x56, 0x40, 0xd6, 0xe9, 
    0xc3, 0x36, 0x2e, 0x15, 0xaf, 0xf1, 0xac, 0xab, 0x09, 0x00, 0x51, 0x7f, 0x1f, 0x15, 0x13, 0x2e, 
    0xd3, 0x33, 0x62, 0xa3, 0x7e, 0x28, 0xfd, 0x62, 0x5e, 0xfa, 0x22, 0xad, 0x7c, 0xd0, 0x03, 0xe4, 
    0x9f, 0x8b, 0x7f, 0xbf, 0x05, 0xbd, 0x77, 0x23, 0x75, 0x9d, 0xae, 0x1f, 0x02, 0x9e, 0x35, 0xc3, 
    0xe0, 0xc3, 0x7b, 0xea, 0x14, 0xa0, 0x6e, 0x69, 0x1a, 0x2d, 0x66, 0x5a, 0x34, 0x19, 0x49, 0x16,
};

static const uint8_t _g_cbc_192_enc[] = {
    0xf1, 0xcf, 0x7b, 0x19, 0xf7, 0xd3, 0x70, 0x0b, 0x8e, 0xa2, 0xb7, 0x98, 0x24, 0x29, 0xad, 0x9f, 
    0xcf, 0x3f, 0xce, 0x8f, 0x2f, 0xa1, 0x56, 0x1b, 0xb9, 0x7c, 0xd9, 0x48, 0x1f, 0x51, 0x89, 0x62, 
    0xa7, 0xb2, 0xeb, 0x04, 0xff, 0xd1, 0x64, 0x55, 0x1c, 0x5d, 0x52, 0x79, 0x16, 0x6a, 0x5e, 0xfb, 
    0xae, 0x76, 0x26, 0xd4, 0x04, 0xfb, 0x04, 0x81, 0x40, 0x9f, 0x75, 0xb7, 0x9f, 0xaa, 0xba, 0xcb, 
    0xc1, 0xcd, 0xb9, 0x53, 0x97, 0x3d, 0xc2, 0xba, 0xd3, 0x85, 0xd0, 0x6d, 0x19, 0x30, 0xcf, 0x78, 
    0x41, 0x72, 0xee, 0xce, 0x53, 0x5e, 0xf1, 0x67, 0x4a, 0x04, 0x2d, 0x28, 0x53, 0x42, 0x78, 0xa7,
};

static const uint8_t _g_cbc_256_enc[] = {
    0x96, 0xd9, 0x7d, 0x67, 0x72, 0xf8, 0x08, 0x87, 0x87, 0x90, 0xc7, 0x0b, 0x0d, 0xef, 0xf5, 0xa9, 
    0x96, 0x55, 0xab, 0x84, 0xee, 0x71, 0x13, 0x10, 0xe0, 0x22, 0xd1, 0x6a, 0xa3, 0x59, 0x3c, 0x48, 
    0xbe, 0xd1, 0x72, 0xda, 0x9e, 0x07, 0x03, 0x58, 0xf0, 0xa9, 0xd3, 0xb4, 0x8f, 0xc0, 0x3f, 0x4a, 
    0x81, 0x0b, 0x4a, 0xe0, 0xf4, 0xfc, 0x78, 0x6e, 0x85, 0x05, 0x73, 0x1a, 0x02, 0xd4, 0x9c, 0xb0, 
    0xe6, 0x39, 0x7a, 0x79, 0xd8, 0x5d, 0x07, 0xba, 0xbb, 0x6d, 0x05, 0x1a, 0x40, 0xfd, 0x86, 0xb7, 
    0x49, 0x07, 0xa4, 0xd5, 0xe7, 0x1a, 0xe5, 0xcb, 0x9c, 0x57, 0xf8, 0xd5, 0x0c, 0x5e, 0xa2, 0x9b, 
};

static const uint8_t _g_cfb8_128_enc[] = {
    0x72, 0xa1, 0xd4, 0x70, 0x9b, 0xf9, 0x27, 0xc9, 0x33, 0x5f, 0x98, 0x03, 0x35, 0xa5, 0x13, 0x63, 
    0xb4, 0x74, 0x30, 0x07, 0xe7, 0x27, 0xd1, 0x19, 0xa9, 0x67, 0x48, 0x23, 0x70, 0x1c, 0xce, 0x1c, 
    0x97, 0x7f, 0xa0, 0x5f, 0x4a, 0xe8, 0x68, 0x29, 0xe8, 0x41, 0x65, 0xc9, 0xab, 0x24, 0xba, 0x3d, 
    0x87, 0x20, 0x88, 0x8f, 0x45, 0x7f, 0x9f, 0x8a, 0xf3, 0x44, 0x5e, 0x44, 0xb7, 0x42, 0xf0, 0x91, 
    0x91, 0x11, 0x29, 0xf9, 0x80, 0x64, 0x3e, 0xfe, 0xeb, 0x5d, 0x5b, 0x71, 0xd1, 0x07, 0x3f, 0xe5, 
    0x9c, 0x79, 0xd6, 0xda, 0x2c, 0x48, 0xfc, 0xba, 0xf9, 0x4d, 0x56, 0x29, 0xb8, 0xd5, 0x26, 0x8b, 
    0xd5, 0xfd, 0xd5, 0x48, 0xcb, 0xdb, 0x75, 0x01, 0xce, 0x2c, 0x53, 0x6b, 0xe1, 0x6b, 0xd7, 0x37, 
    0x5f, 0x5d, 0xb9, 0xa5, 0xcc, 0x0a, 0x26, 0x51, 0x1a, 0x81, 0xa0, 0x2d, 0x96, 0xd0, 0x2f, 0xb4, 
    0x19, 0xcb, 0x57, 0x13, 0x26, 0x33, 0xd9, 0x40, 0x7f, 0xcd, 0x49, 0xf8, 0xde,
};

static const uint8_t _g_cfb8_192_enc[] = {
    0xa3, 0x7a, 0x97, 0x12, 0x4f, 0x26, 0x9d, 0xca, 0x14, 0xbf, 0x07, 0x1d, 0x06, 0xfe, 0x36, 0x81, 
    0x19, 0x66, 0x6c, 0xa6, 0x4e, 0xf5, 0x44, 0x3e, 0x3d, 0x4f, 0xa8, 0xc9, 0xf0, 0xd3, 0xf4, 0x8b, 
    0xa4, 0x34, 0x36, 0x61, 0xb5, 0xbf, 0x70, 0x3d, 0x47, 0x9a, 0x59, 0x94, 0xff, 0x49, 0xd1, 0x12, 
    0x72, 0xe6, 0x4a, 0x1d, 0x27, 0xdf, 0xf9, 0x53, 0xb6, 0xde, 0x65, 0xd2, 0x1f, 0x6e, 0x0a, 0x28, 
    0x1e, 0xc8, 0xf1, 0xca, 0x35, 0xbc, 0x62, 0xe9, 0x13, 0x14, 0x69, 0x87, 0x3c, 0x20, 0xa0, 0xac, 
    0x9b, 0xe1, 0x2c, 0x4b, 0x86, 0x39, 0xec, 0x4a, 0xe6, 0x13, 0x5f, 0xbe, 0x14, 0x0b, 0xc4, 0xdf, 
    0x7f, 0xe8, 0x77, 0xa9, 0xe5, 0x48, 0x53, 0x95, 0xf8, 0xd8, 0x15, 0x18, 0xb6, 0xa9, 0x58, 0x56, 
    0x41, 0xdb, 0x65, 0x40, 0xa3, 0xf4, 0xd0, 0x96, 0x1e, 0x3e, 0x2f, 0xce, 0x4e, 0xbe, 0xca, 0x26, 
    0x66, 0x37, 0x0d, 0xf6, 0x6d, 0xff, 0x30, 0x32, 0x5e, 0x25, 0x26, 0xe8, 0xf8, 
};

static const uint8_t _g_cfb8_256_enc[] = {
    0xd9, 0x0c, 0xe6, 0x45, 0xbc, 0x1b, 0x90, 0x8a, 0x86, 0x00, 0x93, 0x94, 0xd8, 0xaa, 0x53, 0xd8, 
    0x00, 0xf5, 0xe1, 0xbb, 0xd3, 0x97, 0x6f, 0x29, 0x1b, 0x94, 0x6e, 0x75, 0x75, 0xec, 0x53, 0x44, 
    0x33, 0x61, 0x8e, 0x4c, 0x4a, 0xe6, 0x10, 0xb9, 0xd7, 0x3c, 0xfe, 0x69, 0xf2, 0xf7, 0x65, 0xf1, 
    0x56, 0x18, 0x2a, 0xe3, 0x6d, 0xd7, 0x61, 0x02, 0xe9, 0x89, 0x0e, 0xd8, 0x74, 0x6a, 0xe6, 0xdc, 
    0x23, 0xb4, 0xd9, 0x05, 0x59, 0x42, 0x7a, 0x82, 0x56, 0xbd, 0xc2, 0xa0, 0xea, 0x6a, 0x0b, 0xb3, 
    0xf6, 0x68, 0x86, 0x4f, 0x0d, 0x0f, 0xa7, 0x8f, 0x17, 0x7f, 0x97, 0x99, 0xd9, 0xe5, 0xb0, 0xd6, 
    0xf1, 0x1b, 0x61, 0xf6, 0x89, 0x98, 0xda, 0x09, 0xc5, 0x67, 0xbd, 0xe2, 0x14, 0x2a, 0xf3, 0x5b, 
    0x76, 0xf4, 0x99, 0x9c, 0xc0, 0xa0, 0xcb, 0x70, 0xaf, 0x24, 0xab, 0xd6, 0x68, 0x02, 0x67, 0x95, 
    0xb8, 0xf4, 0xa6, 0xcb, 0x7a, 0x2f, 0x1e, 0x50, 0x18, 0x8f, 0x4a, 0xe5, 0x72, 
};

static const uint8_t _g_ctr_128_enc[] = {
    0x72, 0x29, 0xbe, 0x3a, 0x0a, 0x61, 0x7e, 0xae, 0xca, 0xfa, 0xda, 0xaa, 0x08, 0x65, 0x50, 0x1e, 
    0x35, 0x4d, 0x37, 0x50, 0x3e, 0x77, 0xc5, 0x45, 0xee, 0xbd, 0xbe, 0x7b, 0x5e, 0x90, 0xb3, 0xe6, 
    0x1d, 0x2c, 0xb4, 0xd2, 0x16, 0x18, 0xc2, 0x70, 0x84, 0x5d, 0x5a, 0x75, 0xff, 0x7f, 0x23, 0xc4, 
    0xd7, 0xec, 0x7f, 0xfc, 0xb3, 0x3f, 0x5e, 0x9b, 0x38, 0x7a, 0x94, 0x21, 0xa6, 0x32, 0x08, 0x77, 
    0xb2, 0x60, 0x78, 0xc0, 0x8b, 0x79, 0xd4, 0x88, 0x82, 0x5f, 0xa2, 0xf9, 0x1d, 0xf8, 0x75, 0xe6, 
    0x69, 0x41, 0xd1, 0x76, 0x46, 0x95, 0xae, 0xc4, 0xec, 0x4f, 0x13, 0x1e, 0x56, 0xfb, 0x99, 0xa7, 
    0x72, 0x89, 0x2e, 0xcf, 0xf4, 0x16, 0x0b, 0x02, 0xf4, 0x8b, 0x7b, 0xed, 0xf5, 0x38, 0xe9, 0xd6, 
    0xb9, 0x8a, 0xe3, 0xd9, 0x90, 0xde, 0xdb, 0xe2, 0x55, 0xa1, 0x51, 0x19, 0xca, 0x5e, 0xc1, 0x78, 
    0x8a, 0xf5, 0x7b, 0xab, 0xcb, 0xd1, 0x3b, 0xb9, 0xba, 0x4d, 0xc5, 0xea, 0x55,
};

static const uint8_t _g_ctr_192_enc[] = {
    0xa3, 0x5b, 0xe4, 0x76, 0xa0, 0x0a, 0x89, 0x55, 0xad, 0xfd, 0xb9, 0x92, 0x27, 0xaa, 0x7b, 0x8d, 
    0xf5, 0x81, 0xce, 0xc4, 0xe4, 0x43, 0x14, 0x39, 0x4d, 0x57, 0x6a, 0x7d, 0x4a, 0x7c, 0xd8, 0x04, 
    0xa4, 0xcc, 0xe5, 0x29, 0xd8, 0x40, 0x56, 0x46, 0xc8, 0x68, 0x53, 0xd1, 0xa0, 0xd1, 0xad, 0x1d, 
    0x78, 0x4d, 0xa9, 0x49, 0x0e, 0x7b, 0x02, 0x57, 0x99, 0x34, 0x96, 0x7b, 0xe8, 0xfe, 0x26, 0xdf, 
    0x3a, 0xb9, 0xa4, 0x8b, 0x6f, 0x1f, 0x07, 0x25, 0xd2, 0x70, 0x7d, 0x02, 0xa5, 0xee, 0x23, 0x8f, 
    0x49, 0xcb, 0x34, 0x95, 0x11, 0x83, 0x1c, 0x82, 0x35, 0x2b, 0xf2, 0xb4, 0x76, 0x78, 0xdc, 0xa7, 
    0xea, 0x43, 0x9e, 0xad, 0x81, 0xa7, 0x67, 0x87, 0x46, 0xa7, 0xa6, 0xfb, 0xf1, 0x95, 0x04, 0xa6, 
    0x44, 0x78, 0xdc, 0x3c, 0x87, 0x29, 0xc7, 0xfe, 0x52, 0x6b, 0x05, 0x22, 0xbd, 0x71, 0x7b, 0x58, 
    0xb1, 0xc9, 0x34, 0x4b, 0xc3, 0xba, 0x8f, 0x9f, 0xd6, 0xed, 0xf2, 0x49, 0x61,
};

static const uint8_t _g_ctr_256_enc[] = {
    0xd9, 0x14, 0xdd, 0x2c, 0x76, 0x15, 0xfd, 0xae, 0x3c, 0x15, 0x4d, 0x0d, 0x5d, 0xd5, 0x0e, 0x30, 
    0xd4, 0x5d, 0x0a, 0xa7, 0xa8, 0xb5, 0x8d, 0xbf, 0xd7, 0x84, 0xd6, 0x0d, 0x13, 0x11, 0x86, 0x9d, 
    0x43, 0x80, 0xcf, 0x5f, 0x65, 0xe9, 0x8a, 0x36, 0x2e, 0x72, 0x37, 0xf2, 0xaa, 0x2a, 0xbe, 0xf5, 
    0xeb, 0x4d, 0xcb, 0x2a, 0x8b, 0xf7, 0x7e, 0x19, 0xea, 0x27, 0x4e, 0xb6, 0x10, 0xdf, 0x99, 0x28, 
    0xbf, 0x4f, 0x07, 0xd0, 0xf6, 0x74, 0x14, 0x42, 0xa6, 0x5a, 0x22, 0x33, 0xfc, 0xe2, 0x41, 0x01, 
    0x34, 0xd4, 0x08, 0x2d, 0x16, 0x34, 0xd8, 0x7a, 0xf4, 0x08, 0xfb, 0x2f, 0xe0, 0xe1, 0xb8, 0xf1, 
    0x9d, 0x26, 0x4b, 0xac, 0x9d, 0x66, 0x59, 0x2a, 0x0e, 0xd6, 0x8d, 0xd5, 0x38, 0x9d, 0x51, 0xd9, 
    0xaa, 0x44, 0x6d, 0xda, 0xd2, 0xf5, 0x3e, 0xb9, 0xc5, 0x23, 0x20, 0xf6, 0xb1, 0x8a, 0x4e, 0x21, 
    0x06, 0x1f, 0x14, 0x04, 0xde, 0x74, 0xaf, 0x05, 0x2d, 0x76, 0xca, 0x69, 0xed, 
};

static int _aes_ecb_encrypt_decrypt(uint32_t idx, bool is_enc, size_t key_len,
                                    uint8_t *src, uint8_t *dst, void *aes_ctx)
{
    ls_hal_crypt_result result;

    result = ls_hal_aes_ecb_init(aes_ctx, is_enc, (uint8_t *)_g_aes_key, key_len);
    if (result != HAL_CRYPT_SUCCESS) {
        if (result != HAL_CRYPT_NOSUPPORT)
            HAL_TEST_ERR("aes_ecb init fail(%08x)\n", result);
        goto _out;
    }

    result = ls_hal_aes_ecb_process(aes_ctx, src, dst, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_ecb process 1th fail(%08x)", result);
        goto _out;
    }
    result = ls_hal_aes_ecb_process(aes_ctx, src + 16, dst + 16, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_ecb process 2th fail(%08x)", result);
        goto _out;
    }
    result = ls_hal_aes_ecb_process(aes_ctx, src + 32, dst + 32, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_ecb process 3th fail(%08x)", result);
        goto _out;
    }

    result = ls_hal_aes_ecb_process(aes_ctx, src + 48, dst + 48, TEST_ECB_SIZE - 48);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_ecb process 4th fail(%08x)", result);
        goto _out;
    }

_out:
    if (result == HAL_CRYPT_NOSUPPORT) {
        HAL_TEST_INF("[WARN] aes ecb hal not supported!\n");
        result = HAL_CRYPT_SUCCESS;
    }

    return result;
}

static int _aes_cbc_encrypt_decrypt(uint32_t idx, bool is_enc, size_t key_len,
                                    uint8_t *src, uint8_t *dst, void *aes_ctx)
{
    ls_hal_crypt_result  result;

    result = ls_hal_aes_cbc_init(aes_ctx, is_enc, (uint8_t *)_g_aes_key, key_len, (uint8_t *)_g_aes_iv);
    if (result != HAL_CRYPT_SUCCESS) {
        if (result != HAL_CRYPT_NOSUPPORT)
            HAL_TEST_ERR("aes_cbc init fail(%08x)\n", result);
        goto _out;
    }

    result = ls_hal_aes_cbc_process(aes_ctx, src, dst, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cbc process 1th fail(%08x)", result);
        goto _out;
    }
    result = ls_hal_aes_cbc_process(aes_ctx, src + 16, dst + 16, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cbc process 2th fail(%08x)", result);
        goto _out;
    }
    result = ls_hal_aes_cbc_process(aes_ctx, src + 32, dst + 32, 16);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cbc process 3th fail(%08x)", result);
        goto _out;
    }

    result = ls_hal_aes_cbc_process(aes_ctx, src + 48, dst + 48, TEST_CBC_SIZE - 48);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cbc process 4th fail(%08x) \n", result);
    }

_out:
    if (result == HAL_CRYPT_NOSUPPORT) {
        HAL_TEST_INF("[WARN] aes cbc hal not supported!\n");
        result = HAL_CRYPT_SUCCESS;
    }

    return result;
}

static int _aes_ctr_encrypt_decrypt(uint32_t idx, bool is_enc, size_t key_len,
                                    uint8_t *src, uint8_t *dst, void *aes_ctx)
{
    ls_hal_crypt_result result = 0;

    result = ls_hal_aes_ctr_init(aes_ctx, is_enc, (uint8_t *)_g_aes_key, key_len, (uint8_t *)_g_aes_iv);
    if (result != HAL_CRYPT_SUCCESS) {
        if (result != HAL_CRYPT_NOSUPPORT)
            HAL_TEST_ERR("aes_ctr init fail(%08x)\n", result);
        goto _out;
    }
    if (is_enc) {
        result = ls_hal_aes_ctr_process(aes_ctx, src, dst, 13);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 1th fail(%08x)", result);
            goto _out;
        }
        result = ls_hal_aes_ctr_process(aes_ctx, src + 13, dst + 13, 27);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 2th fail(%08x)", result);
            goto _out;
        }
        result = ls_hal_aes_ctr_process(aes_ctx, src + 40, dst + 40, 8);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 3th fail(%08x)", result);
            goto _out;
        }

        result = ls_hal_aes_ctr_process(aes_ctx, src + 48, dst + 48, TEST_CTR_SIZE - 48);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 4th fail(%08x)", result);
            goto _out;
        }
    } else {
        result = ls_hal_aes_ctr_process(aes_ctx, src, dst, 8);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 1th fail(%08x)", result);
            goto _out;
        }
        result = ls_hal_aes_ctr_process(aes_ctx, src + 8, dst + 8, 30);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 2th fail(%08x)", result);
            goto _out;
        }
        result = ls_hal_aes_ctr_process(aes_ctx, src + 38, dst + 38, 10);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 3th fail(%08x)", result);
            goto _out;
        }

        result = ls_hal_aes_ctr_process(aes_ctx, src + 48, dst + 48, TEST_CTR_SIZE - 48);
        if (result != HAL_CRYPT_SUCCESS) {
            HAL_TEST_ERR("aes_ctr process 4th fail(%08x)", result);
        }
    }
_out:
    if (result == HAL_CRYPT_NOSUPPORT) {
        HAL_TEST_INF("[WARN] aes ctr hal not supported!\n");
        result = HAL_CRYPT_SUCCESS;
    }

    return result;
}

static int _aes_cfb_encrypt_decrypt(uint32_t idx, bool is_enc, size_t key_len,
                                    uint8_t *src, uint8_t *dst, void *aes_ctx)
{
    ls_hal_crypt_result result = 0;

    result = ls_hal_aes_cfb_init(aes_ctx, is_enc, (uint8_t *)_g_aes_key, key_len, (uint8_t *)_g_aes_iv);
    if (result != HAL_CRYPT_SUCCESS) {
        if (result != HAL_CRYPT_NOSUPPORT)
            HAL_TEST_ERR("aes_cfb init fail(%08x)\n", result);
        goto _out;
    }

    result = ls_hal_aes_cfb_process(aes_ctx, src, dst, 13);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cfb process 1th fail(%08x)", result);
    }

    result = ls_hal_aes_cfb_process(aes_ctx, src + 13, dst + 13, 27);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cfb process 2th fail(%08x)", result);
    }

    result = ls_hal_aes_cfb_process(aes_ctx, src + 40, dst + 40, 8);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cfb process 3th fail(%08x)", result);
    }

    result = ls_hal_aes_cfb_process(aes_ctx, src + 48, dst + 48, TEST_CFB_SIZE - 48);
    if (result != HAL_CRYPT_SUCCESS) {
        HAL_TEST_ERR("aes_cfb process 4th fail(%08x)", result);
    }

_out:
    if (result == HAL_CRYPT_NOSUPPORT) {
        HAL_TEST_INF("[WARN] aes cfb hal not supported!\n");
        result = HAL_CRYPT_SUCCESS;
    }

    return result;
}

static int _aes_ecb_test(void)
{
    int               ret = 0;
    size_t            i, key_len;
    void *            aes_ctx = NULL;
    size_t            aes_ctx_size;
    uint8_t *         ecb_enc_exp;
    uint8_t           plaintext[TEST_ECB_SIZE];
    uint8_t           ciphertext[TEST_ECB_SIZE];

    aes_ctx_size = ls_hal_aes_ecb_get_size();
    if (aes_ctx_size <= 0) {
        HAL_TEST_ERR("get ctx size fail(%08x)\n");
        return -1;
    }

    aes_ctx = ls_osa_malloc(aes_ctx_size);
    if (aes_ctx == NULL) {
        HAL_TEST_ERR("kmalloc(%d) fail\n", (int)aes_ctx_size);
        ret = -1;
        goto cleanup;
    }

    for (i = 0; i < 3; i++) {
        if (i == 0) {
            key_len     = 16;
            ecb_enc_exp = (uint8_t *)_g_ecb_128_enc;
        } else if (i == 1) {
            key_len     = 24;
            ecb_enc_exp = (uint8_t *)_g_ecb_192_enc;
        } else {
            key_len     = 32;
            ecb_enc_exp = (uint8_t *)_g_ecb_256_enc;
        }

        /* encrypt no pad */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_ecb_encrypt_decrypt(i, true, key_len, (uint8_t *)_g_test_data,
                                       ciphertext, aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ecb encrypt fail!\n");
            goto cleanup;
        }

        /* decrypt no pad */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_ecb_encrypt_decrypt(i, false, key_len, ciphertext, plaintext,
                                       aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ecb decrypt fail!\n");
            goto cleanup;
        }

        /* check result no pad */
        if (memcmp(ciphertext, ecb_enc_exp, TEST_ECB_SIZE) ||
            memcmp(plaintext, (uint8_t *)_g_test_data, TEST_ECB_SIZE)) {
            hal_dump_data("plaintext", plaintext, TEST_ECB_SIZE);
            hal_dump_data("ciphertext", ciphertext, TEST_ECB_SIZE);
            HAL_TEST_ERR("AES_ECB_%d test fail!\n", (int)key_len << 3);
            ret = -1;
        } else {
            HAL_TEST_INF("AES_ECB_%d test success!\n", (int)key_len << 3);
        }
    }

cleanup:
    if (aes_ctx) {
        ls_osa_free(aes_ctx);
        aes_ctx = NULL;
    }

    return ret;
}

static int _hal_aes_cbc_test(void)
{
    int               ret;
    ls_hal_crypt_result result = 0;
    size_t            i, key_len;
    void *            aes_ctx = NULL;
    size_t            aes_ctx_size;
    uint8_t *         cbc_enc_exp;
    uint8_t           plaintext[TEST_CBC_SIZE];
    uint8_t           ciphertext[TEST_CBC_SIZE];

    aes_ctx_size = ls_hal_aes_cbc_get_size();
    if (aes_ctx_size == 0) {
        HAL_TEST_ERR("get ctx size fail(%08x)\n", result);
        return HAL_CRYPT_ERROR;
    }

    aes_ctx = ls_osa_malloc(aes_ctx_size);
    if (aes_ctx == NULL) {
        HAL_TEST_ERR("malloc(%d) fail\n", (int)aes_ctx_size);
        ret = HAL_CRYPT_OUTOFMEM;
        goto cleanup;
    }

    for (i = 0; i < 3; i++) {
        if (i == 0) {
            key_len     = 16;
            cbc_enc_exp = (uint8_t *)_g_cbc_128_enc;
        } else if (i == 1) {
            key_len     = 24;
            cbc_enc_exp = (uint8_t *)_g_cbc_192_enc;
        } else {
            key_len     = 32;
            cbc_enc_exp = (uint8_t *)_g_cbc_256_enc;
        }

        /* encrypt nopad */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_cbc_encrypt_decrypt(i, true, key_len, (uint8_t *)_g_test_data,
                                       ciphertext, aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("cbc encrypt fail!\n");
            goto cleanup;
        }

        /* decrypt no pad */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_cbc_encrypt_decrypt(i, false, key_len, ciphertext, plaintext,
                                       aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("cbc decrypt fail!\n");
            goto cleanup;
        }

        /* check result no pad */
        if (memcmp(ciphertext, cbc_enc_exp, TEST_CBC_SIZE) ||
            memcmp(plaintext, (uint8_t *)_g_test_data, TEST_CBC_SIZE)) {
            hal_dump_data("plaintext", plaintext, TEST_CBC_SIZE);
            hal_dump_data("ciphertext", ciphertext, TEST_CBC_SIZE);
            HAL_TEST_ERR("AES_CBC_%d test fail!\n", (int)key_len << 3);
        } else {
            HAL_TEST_INF("AES_CBC_%d test success!\n", (int)key_len << 3);
        }
    }

cleanup:
    if (aes_ctx) {
        ls_osa_free(aes_ctx);
        aes_ctx = NULL;
    }
    return result;
}

static int _hal_aes_ctr_test(void)
{
    int               ret;
    ls_hal_crypt_result result = 0;
    size_t            i, key_len;
    void *            aes_ctx = NULL;
    size_t            aes_ctx_size;
    uint8_t *         ctr_enc_exp;
    uint8_t           plaintext[TEST_CTR_SIZE];
    uint8_t           ciphertext[TEST_CTR_SIZE];

    aes_ctx_size = ls_hal_aes_ctr_get_size();
        if (aes_ctx_size == 0) {
            HAL_TEST_ERR("get ctx size fail(%08x)\n", result);
            return HAL_CRYPT_ERROR; 
        }

        aes_ctx = ls_osa_malloc(aes_ctx_size);
        if (aes_ctx == NULL) {
            HAL_TEST_ERR("malloc(%d) fail\n", (int)aes_ctx_size);
            return HAL_CRYPT_OUTOFMEM;
        }

    for (i = 0; i < 3; i++) {
        if (i == 0) {
            key_len     = 16;
            ctr_enc_exp = (uint8_t *)_g_ctr_128_enc;
        } else if (i == 1) {
            key_len     = 24;
            ctr_enc_exp = (uint8_t *)_g_ctr_192_enc;
        } else {
            key_len     = 32;
            ctr_enc_exp = (uint8_t *)_g_ctr_256_enc;
        }

        /* encrypt */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_ctr_encrypt_decrypt(i, true, key_len, (uint8_t *)_g_test_data,
                                       ciphertext, aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ctr encrypt fail!\n");
            goto cleanup;      
        }

        /* decrypt */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_ctr_encrypt_decrypt(i, false, key_len, ciphertext, plaintext,
                                       aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ctr decrypt fail!\n");
            goto cleanup;
        }

        /* check result */
        if (memcmp(plaintext, (uint8_t *)_g_test_data, TEST_CTR_SIZE) ||
            memcmp(ciphertext, ctr_enc_exp, TEST_CTR_SIZE)) {
            hal_dump_data("plaintext", plaintext, TEST_CTR_SIZE);
            hal_dump_data("ciphertext", ciphertext, TEST_CTR_SIZE);
            HAL_TEST_ERR("AES_CTR_%d test fail!\n", (int)key_len << 3);
        } else {
            HAL_TEST_INF("AES_CTR_%d test success!\n", (int)key_len << 3);
        }
    }

cleanup:
    if (aes_ctx) {
        ls_osa_free(aes_ctx);
        aes_ctx = NULL;
    }

    return result;
}

static int _hal_aes_cfb_test(void)
{
    int               ret;
    ls_hal_crypt_result result = 0;
    size_t            i, key_len;
    void *            aes_ctx = NULL;
    size_t            aes_ctx_size;
    uint8_t *         cfb_enc_exp;
    uint8_t           plaintext[TEST_CFB_SIZE];
    uint8_t           ciphertext[TEST_CFB_SIZE];

    aes_ctx_size = ls_hal_aes_cfb_get_size();
    if (aes_ctx_size == 0) {
        HAL_TEST_ERR("get ctx size fail(%08x)\n", result);
        return HAL_CRYPT_ERROR;
    }

    aes_ctx = ls_osa_malloc(aes_ctx_size);
    if (aes_ctx == NULL) {
        HAL_TEST_ERR("malloc(%d) fail\n", (int)aes_ctx_size);
        return HAL_CRYPT_OUTOFMEM;
    }


    for (i = 0; i < 3; i++) {
        if (i == 0) {
            key_len = 16;
            cfb_enc_exp = (uint8_t *)_g_cfb8_128_enc;
        } else if (i == 1) {
            key_len = 24;
            cfb_enc_exp = (uint8_t *)_g_cfb8_192_enc;
        } else {
            key_len = 32;
            cfb_enc_exp = (uint8_t *)_g_cfb8_256_enc;
        }

        /* encrypt */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_cfb_encrypt_decrypt(i, true, key_len, (uint8_t *)_g_test_data,
                                       ciphertext, aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ctr encrypt fail!\n");
            goto cleanup;
        }

        /* decrypt */
        memset(aes_ctx, 0, aes_ctx_size);
        ret = _aes_cfb_encrypt_decrypt(i, false, key_len, ciphertext,
                                       plaintext, aes_ctx);
        if (ret < 0) {
            HAL_TEST_ERR("ctr decrypt fail!\n");
            goto cleanup;
        }

        /* check result */
        if (memcmp(plaintext, (uint8_t *)_g_test_data, TEST_CFB_SIZE) ||
            memcmp(ciphertext, cfb_enc_exp, TEST_CFB_SIZE)) {
            hal_dump_data("plaintext", plaintext, TEST_CFB_SIZE);
            hal_dump_data("ciphertext", ciphertext, TEST_CFB_SIZE);
            HAL_TEST_ERR("AES_CFB_%d test fail!\n", (int)key_len << 3);
        } else {
            HAL_TEST_INF("AES_CFB_%d test success!\n", (int)key_len << 3);
        }
    }

cleanup:
    if (aes_ctx) {
        ls_osa_free(aes_ctx);
        aes_ctx = NULL;
    }

    return result;
}

int hal_aes_test(void)
{
    int ret = 0;

    HAL_TEST_INF("HAL AES ECB Test:\n");
    ret = _aes_ecb_test();
    if (ret < 0) {
        if (ret != HAL_CRYPT_NOSUPPORT)
            goto _out;
        ret = 0;
        HAL_TEST_INF("not support, continue..\n");
    }

    HAL_TEST_INF("HAL AES CBC Test:\n");
    ret = _hal_aes_cbc_test();
    if (ret < 0) {
        if (ret != HAL_CRYPT_NOSUPPORT)
            goto _out;
        ret = 0;
        HAL_TEST_INF("not support, continue..\n");
    }

    HAL_TEST_INF("HAL AES CTR Test:\n");
    ret = _hal_aes_ctr_test();
    if (ret < 0) {
        if (ret != HAL_CRYPT_NOSUPPORT)
            goto _out;
        ret = 0;
        HAL_TEST_INF("not support, continue..\n");
    }

    HAL_TEST_INF("HAL AES CFB Test:\n");
    ret = _hal_aes_cfb_test();
    if (ret < 0) {
        if (ret != HAL_CRYPT_NOSUPPORT)
            goto _out;
        ret = 0;
        HAL_TEST_INF("not support, continue..\n");
    }

_out:
    if (0 == ret) {
        HAL_TEST_INF("=============================> HAL AES Test Pass.\n\n");
    } else {
        HAL_TEST_INF("=============================> HAL AES Test Fail.\n\n");
    }

    return ret;
}

#endif  /* CONFIG_HAL_CRYPTO */

